package cn.itsource.basic.util;

import cn.itsource.basic.config.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 专门用来给前端返回信息的
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "交互信息对象")
public class AjaxResult {
    //成功失败大状态
    @ApiModelProperty(value = "true:成功, false:失败")
    private Boolean success = true;

    //提示信息
    @ApiModelProperty(value = "提示信息")
    private String message = "OK";

    //返回数据
    @ApiModelProperty(value = "数据载体")
    private Object data;

    //状态码
    @ApiModelProperty(value = "具体状态码")
    private String code = PetConstant.GAOYUXIN_SUCCESS; //魔法值

    /**
     * 返回成功,不携带数据给前端
     */
    public static AjaxResult createSuccess(){
        return new AjaxResult();
    }

    /**
     * 返回成功,要携带数据给前端
     */
    public static AjaxResult createSuccess(Object data){
        return new AjaxResult(true, "OK", data, PetConstant.GAOYUXIN_SUCCESS);
    }

    /**
     * 返回错误
     */
    public static AjaxResult createSuccess(String code, String message){
        return new AjaxResult(false, message, null, code);
    }

    /**
     * 返回错误,错误码默认系统错误
     */
    public static AjaxResult createSuccess(String message){
        return createSuccess(PetConstant.SYSTEM_ERROR, message);
    }
    /**
     * 返回错误
     */
    public static AjaxResult createError(String code, String message) {
        return new AjaxResult(false, message, null, code);
    }

    /**
     * 返回错误,错误码默认系统错误
     */
    public static AjaxResult createError(String message) {
        return createError(Constants.SYSTEM_ERROR, message);
    }

}
